package com.yolo.example.provider;


import com.yolo.example.common.serivce.UserService;
import yolorpc.RpcApplication;
import yolorpc.config.RpcConfig;
import yolorpc.model.ServiceMetaInfo;
import yolorpc.server.tcp.VertxTcpServer;
import yolorpc.registry.LocalRegistry;
import yolorpc.registry.Registry;
import yolorpc.registry.RegistryConfig;
import yolorpc.registry.RegistryFactory;

/**
 * @author yolo
 * @Program RPC
 * @description 服务提供者
 * @create 2025-04-24 15:39
 **/
public class ProviderExample {
    public static void main(String[] args) {
        // 初始化RPC框架
        RpcApplication.init();
        String serviceName = UserService.class.getName();
        // 注册服务
        LocalRegistry.register(serviceName, UserServiceImpl.class);

        RpcConfig rpcConfig = RpcApplication.getRpcConfig();
        RegistryConfig registryConfig = rpcConfig.getRegistryConfig();
        ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();
        serviceMetaInfo.setServiceName(serviceName);
        serviceMetaInfo.setServiceHost(rpcConfig.getServerHost());
        serviceMetaInfo.setServicePort(rpcConfig.getServerPort());
        Registry registry = RegistryFactory.getInstance(registryConfig.getRegistry());
        try {
            registry.register(serviceMetaInfo);
        } catch (Exception e){
            throw new RuntimeException(e);
        }

        // 启动 TCP 服务
        VertxTcpServer vertxTcpServer = new VertxTcpServer();
        vertxTcpServer.doStart(8081);
    }
}
